<?php
/**
 * 模型管理
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: ModelsContrller.class.php v1.0 2014-06-12 17:27:03 $
 */
namespace Admin\Controller;
class ModelsController extends CommonController {
    protected $model;
    protected $service;
    
    public function _initialize() {
        parent::_initialize();
        $this->model = D("Model");
        $this->service = D("Model", 'Service');
    }

    /**
     * 模型列表
     * @return
     */
    public function index(){
        $result = $this->getPagination('Model');
        //var_dump($result);die;
        $this->assign('models', $result['data']);
        $this->assign('models_count', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display();
    }

    /**
     * 模型信息
     * @return
     */
    public function show() {
        $id = I("get.id", 0, "intval");
        if ($id <= 0) {
            $this->error('您需要查看的模型不存在！');
        }

        $model = $this->service->getById($id);
        if (empty($model)) {
            $this->error('您需要查看的模型不存在！');
        }
        //var_dump($model);die;
        $fieldtype = D("Field")->fieldtype;
        $this->assign('fieldtype', $fieldtype);
        $this->assign('model', $model);
        $this->display();
    }

    /**
     * 添加模型
     * @return
     */
    public function add() {
        $row = $this->model->field("id")->order("id desc")->limit(1)->find();
        $newid = $row['id'] + 1;
        if($newid < 10){ 
            $newid = $newid+10;
        }
        $this->assign("newid", $newid);
        $this->display();
    }

    /**
     * 创建模型
     * @return
     */
    public function create() {
        $model = I("post.model", array(), "trim");
        if (!IS_POST || !isArrayAndNotEmpty($model)) {
            return $this->errorReturn('无效的操作！');
        }

        $model = array_map('trim', $_POST['model']);

        // 检查数据是否合法
        $result = $this->service->checkModel($model);
        if (false === $result['status']) {
            return $this->errorReturn($result['data']['error']);
        }
        // 添加数据
        $result = $this->service->add($model);
        if (false === $result['status']) {
            return $this->errorReturn('系统出错了！');
        }

        $this->successReturn("添加模型 <b>{$model['name']}</b> 成功！",
                             U('Models/index'));
    }

    /**
     * 检查模型名可用性
     * @return
     */
    public function checkModelName() {
        $result = D('Model', 'Service')->checkModelName($_GET['model_name'],
                                                        $_GET['id']);
        if ($result['status']) {
            return $this->successReturn('模型名称可用！');
        }

        return $this->errorReturn($result['data']['error']);
    }

    /**
     * 检查数据表名可用性
     * @return
     */
    public function checkTblName($tableName = null) {
        $tblName = isset($tableName) ? $tableName : I("get.addtable");
        $result = D('Model', 'Service')->checkTblName($tblName, $_GET['id']);

        if ($result['status']) {
            return $this->successReturn('数据表名称可用！');
        }

        return $this->errorReturn($result['data']['error']);
    }


    /**
     * 编辑模型
     * @return
     */
    public function edit() {
        if (!isset($_GET['id'])) {
            $this->error('您需要编辑的模型不存在！');
        }

        $model = M('Model')->getById($_GET['id']);
        if (empty($model)) {
            $this->error('您需要编辑的模型不存在！');
        }

        $this->assign('model', $model);
        $this->display();
    }

    /**
     * 更新模型
     * @return
     */
    public function update() {
        $model = I("post.model", array(), 'trim');
        if (!IS_POST || !isArrayAndNotEmpty($model)) {
            $this->errorReturn('无效的操作！');
        }
        
        if (0 == M('Model')->where(array('id' => $model['id']))->count()) {
            $this->errorReturn('您需要更新的模型不存在！');
        }

        $modelService = D('Model', 'Service');
        $result = $modelService->checkModel($model, $model['id']);
        if (false === $result['status']) {
            return $this->errorReturn($result['data']['error']);
        }

        // 更新数据
        $result = $modelService->update($model);
        if (false === $result['status']) {
            return $this->errorReturn('系统出错了！');
        }

        $this->successReturn("更新模型成功！", U('Models/index'));
    }

    /**
     * 删除模型
     * @return
     */
    public function delete() {
        $id = I("get.id", 0, "intval");
        if ($id <= 0) {
            $this->errorReturn('您需要删除的模型不存在！');
        }

        $model = M('Model')->getById($id);
        if (empty($model)) {
            $this->errorReturn('您需要删除的模型不存在！');
        }

        $result = D('Model', 'Service')->delete($model['id']);
        if (false === $result['status']) {
            return $this->errorReturn('系统出错了！');
        }

        $this->successReturn("删除模型 <b>{$model['name']}</b> 成功！");
    }
    
    /**
     * 切换模型状态
     * @return
     */
    public function toggleStatus() {
        $id = I("post.id", 0, "intval");
        $status = I("post.status", NULL);
        if (!isset($id)
            || !$this->service->existModel($id)) {
            return $this->errorReturn('无效的操作！');
        }
        $modelInfo = $this->service->getById($id, false);
        if($modelInfo['nid'] == 'message'){
            return $this->errorReturn('无效的操作！');
        }
        if ($status == 'yes') {
            $this->service->setStatus($id, 1);
        } else if($status === 'no'){
            $this->service->setStatus($id, 0);
        }else{
            return $this->errorReturn('无效的操作！');
        }

        $info = $status === 'no' ? '禁用成功！' : '启用成功！';
        $this->successReturn($info);
    }
}